﻿// ModuleDialog.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "ModuleDialog.h"
#include "afxdialogex.h"

#include "ModuleBrowserDialog.h"

// CModuleDialog 对话框

IMPLEMENT_DYNAMIC(CModuleDialog, CDialogEx)

CModuleDialog::CModuleDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_MODULE, pParent)
{

}

CModuleDialog::~CModuleDialog()
{
}

void CModuleDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_MODULE, m_list);
	DDX_Control(pDX, IDOK, m_btnExit);
	DDX_Control(pDX, IDC_BTN_MOD_REGISTER, m_btnAdd);
	DDX_Control(pDX, IDC_BTN_MOD_UNREGISTER, m_btnRemove);
}


BEGIN_MESSAGE_MAP(CModuleDialog, CDialogEx)
	ON_BN_CLICKED(IDC_BTN_MOD_REGISTER, &CModuleDialog::OnBtnModRegister)
	ON_BN_CLICKED(IDC_BTN_MOD_UNREGISTER, &CModuleDialog::OnBtnModUnregister)
END_MESSAGE_MAP()


// CModuleDialog 消息处理程序


BOOL CModuleDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	m_list.ModifyStyle(0, LVS_REPORT | LVS_SINGLESEL);
	m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_AUTOSIZECOLUMNS);

	m_list.InsertColumn(0, "名称", LVCFMT_LEFT, 150);
	m_list.InsertColumn(1, "开发者", LVCFMT_LEFT, 150);
	m_list.InsertColumn(2, "说明", LVCFMT_LEFT, 150);
	m_list.InsertColumn(3, "路径", LVCFMT_LEFT, 150);

	HINSTANCE hInstance = ::AfxGetInstanceHandle();
	HBITMAP hBitmap = ::LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP_ICON_EXIT_32));
	m_btnExit.SetBitmap(hBitmap);
	hBitmap = ::LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP_ICON_ADD_32));
	m_btnAdd.SetBitmap(hBitmap);
	hBitmap = ::LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP_ICON_DELETE_32));
	m_btnRemove.SetBitmap(hBitmap);


	Populate();

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CModuleDialog::OnBtnModRegister()
{
	// TODO: 在此添加控件通知处理程序代码
	
	CModuleBrowserDialog dlg;
	if (dlg.DoModal() == IDOK)
	{
		if (dlg.m_strModName.IsEmpty())
		{
			AfxMessageBox("未选择模块");
		}
		else
		{
			//ck::Module* pModule = NULL;
			ck::ModuleManager* pModManager = ck::ModuleManager::GetInstance();
			if (pModManager->Find(dlg.m_strModName))
			{
				AfxMessageBox("模块已经注册");
			}
			else
			{
				RESULTCODE rc = pModManager->RegisterModule(dlg.m_strModName);
				if (rc != RC_SUCCESS)
				{
					AfxMessageBox("模块注册失败");
				}
				else
				{
					ck::Module* pModule = pModManager->LoadModule(dlg.m_strModName);
					if (pModule)
					{
						Populate(pModule);
					}
				}
			}
		}
	}
}


void CModuleDialog::OnBtnModUnregister()
{
	// TODO: 在此添加控件通知处理程序代码
	AfxMessageBox("注销");
}

void CModuleDialog::Populate(ck::Module* pModule)
{
	int nItem = m_list.InsertItem(m_list.GetItemCount(), pModule->GetID());
	m_list.SetItemText(nItem, 1, pModule->GetDeveloper());
	m_list.SetItemText(nItem, 2, pModule->GetDescription());
	m_list.SetItemText(nItem, 3, "");

}

void CModuleDialog::Populate()
{
	z_uint32 count = 0;
	ck::Module* pModule = NULL;
	ck::ModuleManager* pModManager = ck::ModuleManager::GetInstance();
	
	count = pModManager->GetCount();
	for (z_uint32 i = 0; i < count; i++)
	{
		pModule = pModManager->GetModule(i);
		if (pModule) 
		{
			Populate(pModule);
		}
	}
	
}